package com.project.server.controller;

import com.project.server.common.api.Result;
import com.project.server.dto.FamilyMemberDTO;
import com.project.server.service.FamilyMemberService;
import com.project.server.utils.JwtUtil;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/family-members")
public class FamilyMemberController {

    @Autowired
    private FamilyMemberService familyMemberService;

    @GetMapping("/list")
    public Result<List<Map<String, Object>>> getList(HttpServletRequest request) {
        Long userId = JwtUtil.getUserIdFromRequest(request);
        return Result.success(familyMemberService.getMemberById(userId));
    }

    @PostMapping
    public Result<Void> add(@RequestBody FamilyMemberDTO memberDTO, HttpServletRequest request) {
        Long userId = JwtUtil.getUserIdFromRequest(request);
        memberDTO.setMemberId(userId);
        familyMemberService.addMember(memberDTO);
        return Result.success();
    }

    @DeleteMapping("/{memberId}")
    public Result<Void> remove(@PathVariable Long memberId, HttpServletRequest request) {
        Long userId = JwtUtil.getUserIdFromRequest(request);
        familyMemberService.removeMember(userId, memberId);
        return Result.success();
    }










}
